<template>
  <div class="home">
    <!-- 搜索框 -->
    <div class="search_box">
      <van-search  placeholder="请输入搜索关键词" class="search">
        <template #left>
          <div class="cityList" @click="$router.push('/city')">
            {{$store.state.ctiyName.label}}
            <van-icon name="arrow-down" />
          </div>
        </template>
      </van-search>
      <van-icon name="location-o" color="#fff" @click="$router.push('/map')" />
    </div>
    <!-- 轮播图 -->
    <div class="swipe">
      <van-swipe :autoplay="3000">
        <van-swipe-item v-for="item in imgList" :key="item.id">
          <img v-lazy="'http://liufusong.top:8080'+item.imgSrc" />
        </van-swipe-item>
      </van-swipe>
    </div>
    <div class="grid">
      <van-grid :border='false'>
        <van-grid-item icon=""
        text="文字" />
        <van-grid-item icon=""
        text="文字" />
        <van-grid-item icon=""
        text="文字" />
        <van-grid-item icon=""
        text="文字" />
      </van-grid>
    </div>
    <!-- 租房小组 -->
    <div class="group">
      <h3 class="group_title">
        租房小组
        <span class="more">更多</span>
      </h3>
      <div class="am-grid">
        <van-grid direction="horizontal" :column-num="2" :border='false'>
          <van-grid-item :icon="'http://liufusong.top:8080'+item.imgSrc" v-for="item in gridlist" :key="item.id" >
            <template #text>
              <div>
                <p>{{item.title}}</p>
                <p>{{item.desc}}</p>
              </div>
            </template>
          </van-grid-item>
        </van-grid>
      </div>
    </div>
  </div>
</template>

<script>
import { swiper, groups, Bmap } from '@/api/home.js'
import { Info } from '@/api/area.js'
import { mapState } from 'vuex'
export default {
  name: 'home',
  data () {
    return {
      imgList: {},
      gridlist: [],
      city: [],
      InfoCity: ''
    }
  },
  computed: {
    ...mapState(['ctiyName'])
  },
  created () {
    this.getSwiper()
    this.getBmap()
    this.getInfo()
  },

  methods: {
    async getSwiper () {
      const res = await swiper()
      this.imgList = res
    },
    async getGroups () {
      const res = await groups(this.ctiyName.value)
      this.gridlist = res
    },
    // 本地地址
    async getBmap () {
      const res = await Bmap()
      console.log(res)
      this.InfoCity = res.content.address_detail.city
    },
    // 默认地址
    async getInfo () {
      const res = await Info(this.InfoCity)
      this.city = res
      if (this.ctiyName) {
        this.getGroups()
      } else {
        this.$store.commit('initCity', res)
      }
    }
  }
}
</script>

<style scoped lang='less'>
.home{
  position: relative;
  .search_box{
    position: absolute;
    top: 25px;
    width: 100%;
    padding: 0 10px;
    box-sizing: border-box;
    display: flex;
    justify-content: space-between;
    z-index: 2;
    .search{
      width: 100%;
      height: 34px;
      margin: 0 10px;
      padding: 5px  5px 5px 8px;
      border-radius: 3px;
      background-color: #fff;
    }
    .van-search__content{
      background-color: transparent;
    }
    .cityList{
      font-size: 14px;
    }
    .van-icon{
      line-height: 34px;
    }
  }
  .swipe{
    img{
      width: 375px;
      height: 213px;
      display: block;
    }
  }
  .grid{
    /deep/.van-icon__image{
      width: 60px;
      height: 60px;
    }
    /deep/.van-grid-item__text{
      font-size: 14px;
      font-weight: 400;
      color: #000;
    }
  }
  .group{
    overflow: hidden;
    background-color: #f6f5f6;
    padding:0 10px;
    width: 100%;
    box-sizing: border-box;
    .group_title{
      display: flex;
      justify-content: space-between;
      margin: 15px 0 15px 10px;
      font-size: 15px;
      .more{
        color: #787878;
        font-style: 14px;
        font-weight: 400;
      }
    }
    .van-grid-item{
      width: 172px;
      height: 60px;
      padding: 5px;
      margin-bottom: 10px;
      /deep/.van-grid-item__content--center{
        justify-content: space-evenly;
      }
      /deep/.van-icon__image{
        width: 50px;
        height: 50px;
      }
      p{
        font-size: 12px;
        margin: 0;
      }
    }
  }
}
</style>
